package aeemqx

import (
	"context"
	v1 "devops-super/api/public/v1"
	"devops-super/internal/consts"
	"devops-super/internal/model/mymodel"
	"encoding/base64"
	"errors"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

// GetWebMqttInfo web网页获取mqtt信息 用户和密码都base64化
func (s *sAeemqx) GetWebMqttInfo(ctx context.Context, req *v1.QcpublicMqttInfoReq) (res *v1.QcpublicMqttInfoRes, err error) {
	res = new(v1.QcpublicMqttInfoRes)
	confKey := consts.EnvConfig_Mqtt + "." + consts.EnvConfig_Mqtt_Default
	confString, err := g.Cfg().Get(ctx, confKey)
	if err != nil || confString == nil {
		err = errors.New("read mqtt config empty")
		return
	}
	var conf *mymodel.MqttConnectOption
	err = gconv.Structs(confString, &conf)
	if err != nil || conf == nil {
		err = errors.New("parse mqtt config empty")
		return
	}
	conf.User = base64.StdEncoding.EncodeToString([]byte(conf.User))
	conf.Password = base64.StdEncoding.EncodeToString([]byte(conf.Password))
	res.Info = conf
	return
}
